package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {


	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format> ");
			return;
		} 
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File " + file + " exists, please remove it manually");
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		}
		else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		}
		else System.out.println("Unknown format " + format);
		return;
		
		
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}
	
	private static void saveContactsToCsvFile (List<ContactData> contacts, File file)  throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getfirstName() + "," + contact.getlastName() + "," + contact.getaddress() + "," +
					contact.gethomeNumber() + "," + contact.getmobileNumber() + "," + contact.getworkNumber() + "," + 
					contact.getmail() + ","  + contact.getmail2() + ","  +  contact.getbirthDay() + "," + 
					contact.getbirthMonth() + "," + contact.getbirthYear() + "," + "" + "," + contact.getsecAddress() + "," + contact.getsecHomeNumber() +",[" + "\n");
		}
		writer.close();
		
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
			.withfirstName(part[0])
			.withlastName(part[1])
			.withaddress(part[2])
			.withhomeNumber(part[3])
			.withmobileNumber(part[4])
			.withworkNumber(part[5])
			.withmail(part[6])
			.withmail2(part[7])
			.withbirthDay(part[8])
			.withbirthMonth(part[9])
			.withbirthYear(part[10])
			.withgroup(part[11])
			.withsecAddress(part[12])
			.withsecHomeNumber(part[13]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++){
            Random rnd1 = new Random();
            String day = String.valueOf(1 + rnd1.nextInt(30));
            String strYear = String.valueOf(rnd1.nextInt(100) + 1913);
            ContactData contact = new ContactData()
            .withfirstName(generateRandomString())
            .withlastName(generateRandomString())
            .withaddress(generateRandomString())
            .withhomeNumber(generateRandomString())
            .withmobileNumber(generateRandomString())
            .withworkNumber(generateRandomString())
            .withmail(generateRandomString())
            .withmail2(generateRandomString())
            .withbirthDay(day)
            .withbirthMonth("-")
            .withbirthYear(strYear)
            .withgroup("")
            .withsecAddress(generateRandomString())
            .withsecHomeNumber(generateRandomString());

			list.add(contact);
		}
		return list;
	}
	public static String generateRandomString() {
		Random rnd = new Random();
		if (rnd.nextInt(3) == 0){
			return "";
		}
		else {
			return "test" + rnd.nextInt();
		}
		
	}	
}
